﻿using Nasty.Core.Session;
using Nasty.Core.Entity;
using SqlSugar;
using Nasty.Core.Attributes;

namespace Nasty.Core.Entity.System
{
    /// <summary>
    /// 第三方应用实体类
    /// </summary>
    [SugarTable("SysThirdParty", TableDescription = "第三方应用")]
    public class ThirdParty : StandardEntity<ThirdParty>
    {
        /// <summary>
        /// 名称
        /// </summary>
        [NastyColumn(ColumnName = "Name", ColumnDescription = "名称", Required = true)]
        public string? Name { get; set; }

        /// <summary>
        /// 应用Key
        /// </summary>
        [NastyColumn(ColumnName = "APPKey", IsOnlyIgnoreUpdate = true, ColumnDescription = "应用Key", Required = true)]
        public string? APPKey { get; set; }

        /// <summary>
        /// 授权密钥
        /// </summary>
        [SugarColumn(ColumnName = "AccessKey", ColumnDescription = "授权密钥")]
        public string? SecretKey { get; set; }

        /// <summary>
        /// 上一个Token
        /// </summary>
        [SugarColumn(ColumnName = "PreToken", IsNullable = true, ColumnDescription = "上一个Token")]
        public string? PreToken { get; set; }

        /// <summary>
        /// 当前Token
        /// </summary>
        [SugarColumn(ColumnName = "Token", IsNullable = true, ColumnDescription = "当前Token")]
        public string? Token { get; set; }

        /// <summary>
        /// 当前Token生成时间
        /// </summary>
        [SugarColumn(ColumnName = "TokenTime", IsNullable = true, ColumnDescription = "当前Token生成时间")]
        public DateTime? TokenTime { get; set; }

        /// <summary>
        /// 有效时间 秒
        /// </summary>
        [SugarColumn(ColumnName = "ExpireIn", IsNullable = true, ColumnDescription = "有效时间 秒")]
        public int ExpireIn { get; set; }

        public override void OnPreAdd()
        {
            base.OnPreAdd();

            var accessKey = Guid.NewGuid().ToString("N");
            this.APPKey = accessKey.GetHashCode().ToString("X").ToUpper();

            var secretKey = Guid.NewGuid().ToString("N");
            this.SecretKey = secretKey.GetHashCode().ToString("X").ToUpper();
        }

        public override void OnPreDelete()
        {
            base.OnPreDelete();

            //var db = AppSession.CurrentDb.Value;
        }

    }

}
